<template>
    <div class="list" ref="wrapper">
        <div >
            <div class="area">
                <div class="title border-topbottom">当前城市</div>
                <div class="button-list">
                    <div class="button-wrapper">
                        <div class="button">{{this.city}}</div>
                    </div>
                </div>
            </div>
            <div class="area">
                <div class="title border-topbottom">热门城市</div>
                <div class="button-list">
                    <div class="button-wrapper" v-for='item of hotCities' :key='item.id' @click='handleCityClick(item.name)'>
                        <div class="button">{{item.name}}</div>
                    </div>
                </div>
            </div>
            <div class="area" v-for="(item, key) of cities" :key="key" :ref="key">
                <div class="title border-city">{{key}}</div>
                <div class="item-list-city">
                    <div class="item border-bottom" v-for="innerItem of item" :key="innerItem.id"
                    @click="handleCityClick(innerItem.name)">{{innerItem.name}}
            </div>
        </div>
      </div>
    </div>
    </div>
</template>
<script>
import {mapState, mapMutations} from 'vuex'
import BScroll from 'better-scroll'
export default{
  name: 'CityList',
  props: {
    cities: Object,
    hotCities: Array,
    letter: String
  },
  methods: {
    handleCityClick (city) {
      this.changCity(city)
      this.$router.push('/')
    },
    ...mapMutations(['changCity'])
  },
  mounted () { // dom挂载借宿执行
    // 创建scroll实例属性
    this.scroll = new BScroll(this.$refs.wrapper) // 将this.$refs.wrapper传入
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
    //   console.log(this.letter)
    }
  },
  computed: {
    ...mapState(['city'])
  }
}

</script>
<style lang="stylus" scoped>
    @import '../../../assets/style/varibles.styl'
    .border-topbottom
        &:before
            border-color: #DDDDDD
        &:after
            border-color: #DDDDDD
    .title
        line-height: .44rem
        background: #F5F5F5
        padding: .2rem
        font-size: .28rem
        color: #212121
    .list
        overflow: hidden
        position: absolute
        top: 1.58rem
        left: 0
        right: 0
        bottom: 0
    .button-list
        overflow: hidden
        padding: .1rem
        .button-wrapper
            float: left
            width: 33.3%
            .button
                margin: .1rem .4rem .1rem .2rem
                text-algin: center
                font-size: .3rem
                border: .02rem solid #ccc
                border-radius: .06rem
                padding: .2rem .3rem .2rem .6rem
    .border-alphabet
        &:before
            border-color: #DDDDDD
        &:after
            border-color: #DDDDDD
    .title
        line-height: .44rem
        background: #F5F5F5
        padding: .2rem
        font-size: .28rem
        color: #212121
    .button-list-alphabet
        overflow: hidden
        padding: .1rem
        .button-wrapper
            float: left
            width: 16%
            .button
                margin: .1rem .2rem
                text-algin: center
                font-size: .3rem
                border: .02rem solid #ccc
                padding: .2rem .3rem
    .border-city
        padding-left: .2rem
    .item-list-city
        padding: .1rem .3rem .2rem .3rem
        line-height: .76rem
    .border-bottom
        &:before
            border-color: #DDDDDD
        &:after
            border-color: #DDDDDD
</style>
